<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2005/06/03 22:09:56 $)
// $RCSfile: functions_misc.php,v $ : $Revision: 1.1 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Render the Category Form
*/
function renderCalendarCategoryForm($type)
{
	global $dbCore, $_SWIFT;

	printFormStart();
	printMainTableHeader($_SWIFT["language"]["categorydetails"]);

	printTextRow("title", $_SWIFT["language"]["categorytitle"], $_SWIFT["language"]["desc_categorytitle"], "text", $_POST["title"]);

	$options = array();
	$options[0]["title"] = $_SWIFT["language"]["event"];
	$options[0]["value"] = CATEGORY_EVENT;
	$options[0]["selected"] = true;
	$options[1]["title"] = $_SWIFT["language"]["contact"];
	$options[1]["value"] = CATEGORY_CONTACT;

	printSelectRow("categorytype", $_SWIFT["language"]["categorytype"], $_SWIFT["language"]["desc_categorytype"], $options);

	if ($type == INSERT)
	{
		$displayorder = count($_SWIFT["calendarcategorycache"])+1;
	} else {
		$displayorder = $_POST["displayorder"];
	}
	printNumberRow("displayorder", $_SWIFT["language"]["displayorder"], $_SWIFT["language"]["desc_catdisplayorder"], $displayorder);

	printSubmitRow(iif($type==INSERT, $_SWIFT["language"]["insert"], $_SWIFT["language"]["update"]));

	printMainTableFooter();

	addHiddenField("_m", "teamwork");
	addHiddenField("_a", iif($type==INSERT, "insertcategory", "editcategory"));
	addHiddenField("step", "1");
	if ($type == EDIT)
	{
		addHiddenField("calendarcategoryid", $_POST["calendarcategoryid"]);
	}
	printFormEnd();
}

/**
* Render the Label Form
*/
function renderCalendarLabelForm($type)
{
	global $dbCore, $_SWIFT;

	printFormStart();
	printMainTableHeader($_SWIFT["language"]["labeldetails"]);

	printTextRow("title", $_SWIFT["language"]["labeltitle"], $_SWIFT["language"]["desc_labeltitle"], "text", $_POST["title"]);

	$options = array();
	$options[0]["title"] = $_SWIFT["language"]["event"];
	$options[0]["value"] = STATUS_EVENT;
	$options[0]["selected"] = true;
	$options[1]["title"] = $_SWIFT["language"]["task"];
	$options[1]["value"] = STATUS_TASK;

	printSelectRow("labeltype", $_SWIFT["language"]["labeltype"], $_SWIFT["language"]["desc_labeltype"], $options);

	if ($type == INSERT)
	{
		$displayorder = count($_SWIFT["calendarlabelcache"])+1;
	} else {
		$displayorder = $_POST["displayorder"];
	}
	printNumberRow("displayorder", $_SWIFT["language"]["displayorder"], $_SWIFT["language"]["desc_labeldisplayorder"], $displayorder);

	printColorRow("labelcolorcode", $_SWIFT["language"]["labelcolorcode"], $_SWIFT["language"]["desc_labelcolorcode"], $_POST["labelcolorcode"]);

	printSubmitRow(iif($type==INSERT, $_SWIFT["language"]["insert"], $_SWIFT["language"]["update"]));

	printMainTableFooter();

	addHiddenField("_m", "teamwork");
	addHiddenField("_a", iif($type==INSERT, "insertlabel", "editlabel"));
	addHiddenField("step", "1");
	if ($type == EDIT)
	{
		addHiddenField("calendarlabelid", $_POST["calendarlabelid"]);
	}
	printFormEnd();
}

/**
* Render the Priority Form
*/
function renderCalendarPriorityForm($type)
{
	global $dbCore, $_SWIFT;

	printFormStart();
	printMainTableHeader($_SWIFT["language"]["prioritydetails"]);

	printTextRow("title", $_SWIFT["language"]["prioritytitle"], $_SWIFT["language"]["desc_prioritytitle"], "text", $_POST["title"]);

	if ($type == INSERT)
	{
		$displayorder = count($_SWIFT["calendarprioritycache"])+1;
	} else {
		$displayorder = $_POST["displayorder"];
	}
	printNumberRow("displayorder", $_SWIFT["language"]["displayorder"], $_SWIFT["language"]["desc_prdisplayorder"], $displayorder);

	printColorRow("prioritycolorcode", $_SWIFT["language"]["prioritycolorcode"], $_SWIFT["language"]["desc_prioritycolorcode"], $_POST["prioritycolorcode"]);

	printSubmitRow(iif($type==INSERT, $_SWIFT["language"]["insert"], $_SWIFT["language"]["update"]));

	printMainTableFooter();

	addHiddenField("_m", "teamwork");
	addHiddenField("_a", iif($type==INSERT, "insertpriority", "editpriority"));
	addHiddenField("step", "1");
	if ($type == EDIT)
	{
		addHiddenField("calendarpriorityid", $_POST["calendarpriorityid"]);
	}
	printFormEnd();
}

/**
* Render the Status Form
*/
function renderCalendarStatusForm($type)
{
	global $dbCore, $_SWIFT;

	printFormStart();
	printMainTableHeader($_SWIFT["language"]["statusdetails"]);

	printTextRow("title", $_SWIFT["language"]["statustitle"], $_SWIFT["language"]["desc_statustitle"], "text", $_POST["title"]);

	$options = array();
	$options[0]["title"] = $_SWIFT["language"]["event"];
	$options[0]["value"] = STATUS_EVENT;
	$options[0]["selected"] = true;
	$options[1]["title"] = $_SWIFT["language"]["task"];
	$options[1]["value"] = STATUS_TASK;

	printSelectRow("statustype", $_SWIFT["language"]["statustype"], $_SWIFT["language"]["desc_statustype"], $options);

	if ($type == INSERT)
	{
		$displayorder = count($_SWIFT["calendarstatuscache"])+1;
	} else {
		$displayorder = $_POST["displayorder"];
	}
	printNumberRow("displayorder", $_SWIFT["language"]["displayorder"], $_SWIFT["language"]["desc_statusdisplayorder"], $displayorder);

	printColorRow("statuscolorcode", $_SWIFT["language"]["statuscolorcode"], $_SWIFT["language"]["desc_statuscolorcode"], $_POST["statuscolorcode"]);

	printSubmitRow(iif($type==INSERT, $_SWIFT["language"]["insert"], $_SWIFT["language"]["update"]));

	printMainTableFooter();

	addHiddenField("_m", "teamwork");
	addHiddenField("_a", iif($type==INSERT, "insertstatus", "editstatus"));
	addHiddenField("step", "1");
	if ($type == EDIT)
	{
		addHiddenField("calendarstatusid", $_POST["calendarstatusid"]);
	}
	printFormEnd();
}

/**
* Insert a new Calendar Category
*/
function insertCalendarCategory($categorytype, $title, $displayorder)
{
	global $dbCore;

	$dbCore->query("INSERT INTO `". TABLE_PREFIX ."calendarcategories` (`categorytype`, `title`, `displayorder`) VALUES ('". intval($categorytype) ."', '". $dbCore->escape($title) ."', '".intval($displayorder) ."');");

	rebuildCalendarCategoryCache();

	return true;
}

/**
* Update a new Calendar Category
*/
function updateCalendarCategory($calendarcategoryid, $categorytype, $title, $displayorder)
{
	global $dbCore;

	$dbCore->query("UPDATE `". TABLE_PREFIX ."calendarcategories` SET `categorytype` = '". intval($categorytype) ."', `title` = '". $dbCore->escape($title) ."', `displayorder` = '". intval($displayorder) ."' WHERE `calendarcategoryid` = '". intval($calendarcategoryid) ."';");

	rebuildCalendarCategoryCache();

	return true;
}

/**
* Insert a new Calendar Label
*/
function insertCalendarLabel($labeltype, $title, $labelcolorcode, $displayorder)
{
	global $dbCore;

	$dbCore->query("INSERT INTO `". TABLE_PREFIX ."calendarlabels` (`labeltype`, `title`, `labelcolorcode`, `displayorder`) VALUES ('". intval($labeltype) ."', '". $dbCore->escape($title) ."', '". $dbCore->escape($labelcolorcode) ."', '". intval($displayorder) ."');");

	rebuildCalendarLabelCache();

	return true;
}

/**
* Update Calendar Label
*/
function updateCalendarLabel($calendarlabelid, $labeltype, $title, $labelcolorcode, $displayorder)
{
	global $dbCore;

	$dbCore->query("UPDATE `". TABLE_PREFIX ."calendarlabels` SET `labeltype` = '". intval($labeltype) ."', `title` = '". $dbCore->escape($title) ."', `labelcolorcode` = '". $dbCore->escape($labelcolorcode) ."', `displayorder` = '". intval($displayorder) ."' WHERE `calendarlabelid` = '". intval($calendarlabelid) ."';");

	rebuildCalendarLabelCache();

	return true;
}

/**
* Insert a new Calendar Priority
*/
function insertCalendarPriority($prioritytype, $title, $prioritycolorcode, $displayorder)
{
	global $dbCore;

	$dbCore->query("INSERT INTO `". TABLE_PREFIX ."calendarpriorities` (`prioritytype`, `title`, `prioritycolorcode`, `displayorder`) VALUES ('". intval($prioritytype) ."', '". $dbCore->escape($title) ."', '". $dbCore->escape($prioritycolorcode) ."', '". intval($displayorder) ."');");

	rebuildCalendarPriorityCache();

	return true;
}

/**
* Update Calendar Priority
*/
function updateCalendarPriority($calendarpriorityid, $prioritytype, $title, $prioritycolorcode, $displayorder)
{
	global $dbCore;

	$dbCore->query("UPDATE `". TABLE_PREFIX ."calendarpriorities` SET `prioritytype` = '". intval($prioritytype) ."', `title` = '". $dbCore->escape($title) ."', `prioritycolorcode` = '". $dbCore->escape($prioritycolorcode) ."', `displayorder` = '". intval($displayorder) ."' WHERE `calendarpriorityid` = '". intval($calendarpriorityid) ."';");

	rebuildCalendarPriorityCache();

	return true;
}

/**
* Insert a new calendar status
*/
function insertCalendarStatus($statustype, $title, $statuscolorcode, $displayorder)
{
	global $dbCore;

	$dbCore->query("INSERT INTO `". TABLE_PREFIX ."calendarstatus` (`statustype`, `title`, `statuscolorcode`, `displayorder`) VALUES ('". intval($statustype) ."', '". $dbCore->escape($title) ."', '". $dbCore->escape($statuscolorcode) ."', '". intval($displayorder) ."');");

	rebuildCalendarStatusCache();

	return true;
}

/**
* Update the Calendar Status
*/
function updateCalendarStatus($calendarstatusid, $statustype, $title, $statuscolorcode, $displayorder)
{
	global $dbCore;

	$dbCore->query("UPDATE `". TABLE_PREFIX ."calendarstatus` SET `statustype` = '". intval($statustype) ."', `title` = '". $dbCore->escape($title) ."', `statuscolorcode` = '". $dbCore->escape($statuscolorcode) ."', `displayorder` = '". intval($displayorder) ."' WHERE `calendarstatusid` = '". intval($calendarstatusid) ."';");

	rebuildCalendarStatusCache();

	return true;
}

/**
* Delete a Calendar Status Entry
*/
function deleteCalendarStatus($calendarstatusidlist)
{
	global $dbCore, $_SWIFT;

	if (!_is_array($calendarstatusidlist))
	{
		return false;
	}

	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."calendarstatus` WHERE `calendarstatusid` IN (". buildIN($calendarstatusidlist) .");");

	rebuildCalendarStatusCache();

	return true;
}

/**
* Delete a Calendar Priority Entry
*/
function deleteCalendarPriority($calendarpriorityidlist)
{
	global $dbCore;

	if (!_is_array($calendarpriorityidlist))
	{
		return false;
	}

	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."calendarpriorities` WHERE `calendarpriorityid` IN (". buildIN($calendarpriorityidlist) .");");

	rebuildCalendarPriorityCache();

	return true;
}

/**
* Delete a Calendar Label
*/
function deleteCalendarLabels($calendarlabelidlist)
{
	global $dbCore;

	if (!_is_array($calendarlabelidlist))
	{
		return false;
	}

	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."calendarlabels` WHERE `calendarlabelid` IN (". buildIN($calendarlabelidlist) .");");

	rebuildCalendarLabelCache();

	return true;
}

/**
* Delete a Calendar Category
*/
function deleteCalendarCategory($calendarcategoryidlist)
{
	global $dbCore;

	if (!_is_array($calendarcategoryidlist))
	{
		return false;
	}

	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."calendarcategories` WHERE `calendarcategoryid` IN (". buildIN($calendarcategoryidlist) .");");

	rebuildCalendarCategoryCache();

	return true;
}

/**
* Rebuilds the calendar status cache
*/
function rebuildCalendarStatusCache()
{
	global $dbCore, $_SWIFT, $datastore;

	$_cache = array();
	
	$index = 0;
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."calendarstatus` ORDER BY `displayorder` ASC;", 3);
	while ($dbCore->nextRecord(3))
	{
		$index++;
		$_cache[$dbCore->Record3["calendarstatusid"]] = $dbCore->Record3;
		$_cache[$dbCore->Record3["calendarstatusid"]]["index"] = $index;
	}

	$datastore->update("calendarstatuscache", $_cache);
}

/**
* Rebuilds the calendar priority cache
*/
function rebuildCalendarPriorityCache()
{
	global $dbCore, $_SWIFT, $datastore;

	$_cache = array();
	
	$index = 0;
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."calendarpriorities` ORDER BY `displayorder` ASC;", 3);
	while ($dbCore->nextRecord(3))
	{
		$index++;
		$_cache[$dbCore->Record3["calendarpriorityid"]] = $dbCore->Record3;
		$_cache[$dbCore->Record3["calendarpriorityid"]]["index"] = $index;
	}

	$datastore->update("calendarprioritycache", $_cache);
}

/**
* Rebuilds the calendar label cache
*/
function rebuildCalendarLabelCache()
{
	global $dbCore, $_SWIFT, $datastore;

	$_cache = array();
	
	$index = 0;
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."calendarlabels` ORDER BY `displayorder` ASC;", 3);
	while ($dbCore->nextRecord(3))
	{
		$index++;
		$_cache[$dbCore->Record3["calendarlabelid"]] = $dbCore->Record3;
		$_cache[$dbCore->Record3["calendarlabelid"]]["index"] = $index;
	}

	$datastore->update("calendarlabelcache", $_cache);
}

/**
* Rebuilds the calendar category cache
*/
function rebuildCalendarCategoryCache()
{
	global $dbCore, $_SWIFT, $datastore;

	$_cache = array();
	
	$index = 0;
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."calendarcategories` ORDER BY `displayorder` ASC;", 3);
	while ($dbCore->nextRecord(3))
	{
		$index++;
		$_cache[$dbCore->Record3["calendarcategoryid"]] = $dbCore->Record3;
		$_cache[$dbCore->Record3["calendarcategoryid"]]["index"] = $index;
	}

	$datastore->update("calendarcategorycache", $_cache);
}
?>